import {createRouter, createWebHashHistory} from "vue-router";
import {ElMessage} from 'element-plus'
//定义路由数据
const routeConfig = [
    // 根路径 - 默认跳转到门户首页
    {
        path: '/',
        component: () => import('@/views/portal/index'),
        meta: {title: '图书销售系统 - 首页'}
    },
    //管理端相关路由配置
    {
        path: '/adminlogin',
        component: () => import('@/viewsAdmin/login/index'),
        meta: {title: '管理员登录'}
    }, {
        path: '/admin/layout',
        component: () => import('@/viewsAdmin/layout/index'),
        meta: {title: '管理端框架'},
        //访问框架，默认跳转图书列表
        redirect: '/admin/bookList',
        //框架的子组件
        children: [{
            path: '/admin/bookList',
            component: () => import('@/viewsAdmin/book/bookList'),
            //通过meta描述该路由需要管理员登录才可以访问
            meta: {title: '图书维护', needLogin: true, userType: 'admin'}
        }, {
            path: '/admin/orderList',
            component: () => import('@/viewsAdmin/order/orderList'),
            meta: {title: '订单维护', needLogin: true, userType: 'admin'}
        }, {
            path: '/admin/typeList',
            component: () => import('@/viewsAdmin/bookType/typeList'),
            meta: {title: '图书类型维护', needLogin: true, userType: 'admin'}
        }, {
            path: '/admin/userList',
            component: () => import('@/viewsAdmin/user/userList'),
            meta: {title: '用户维护', needLogin: true, userType: 'admin'}
        }, {
            path: '/admin/adminList',
            component: () => import('@/viewsAdmin/admin/adminList'),
            meta: {title: '管理员维护', needLogin: true, userType: 'admin'}
        }]
    },
    //用户端相关路由配置
    {
        path: '/clientlogin',
        component: () => import('@/viewsClient/login/login'),
        meta: {title: '用户登录'}
    }, {
        path: '/register',
        component: () => import('@/viewsClient/login/register'),
        meta: {title: '用户注册'}
    }, {
        path: '/client/layout',
        component: () => import('@/viewsClient/layout/index'),
        meta: {title: '前段框架'},
        //访问框架默认跳转首页
        redirect: '/client/index',
        children: [{
            path: '/client/index',
            component: () => import('@/viewsClient/index/index'),
            meta: {title: '网站首页'}
        }, {
            path: '/client/bookinfo',
            component: () => import('@/viewsClient/bookinfo/index'),
            meta: {title: '图书详情'}
        }, {
            path: '/client/shopping',
            component: () => import('@/viewsClient/shopping/index'),
            //通过meta描述该路由需要用户登录才可以访问
            meta: {title: '我的购物车', needLogin: true, userType: 'user'}
        }, {
            path: '/client/orderList',
            component: () => import('@/viewsClient/order/orderList'),
            //通过meta描述该路由需要用户登录才可以访问
            meta: {title: '我的订单', needLogin: true, userType: 'user'}
        }]
    },
]
//创建路由
const router = createRouter({
    history: createWebHashHistory(),
    routes: routeConfig
})
//路由全局前置守卫
router.beforeEach((to, from, next) => {
    console.log('to', to)
    console.log('from', from)
    //修改页面标题
    window.document.title = to.meta.title;
    if (to.matched.length != 0) {
        //访问URL能路由匹配上
        if (to.meta.needLogin) {
            console.info('需要登录')
            //如果需要登录,并且存在登录凭证，则进行跳转
            if (sessionStorage.getItem('userCode')) {
                console.info('需要登录，用户已登录')
                if (to.meta.userType != sessionStorage.getItem('userType')) {
                    ElMessage.info("请先登录")
                    console.info('用户身份不匹配，根据用户类型跳转对应登录页面')
                    if (to.meta.userType == 'admin')
                        next('/adminlogin')
                    if (to.meta.userType == 'user')
                        next(`/clientlogin?redirect=${to.fullPath}`)
                    return;
                } else {
                    console.info('用户身份匹配，允许访问')
                    next()
                    return;
                }
            } else {
                ElMessage.info("请先登录")
                console.info('需要登录未登录，根据用户类型跳转对应登录页面')
                if (to.meta.userType == 'admin')
                    next('/adminlogin')
                if (to.meta.userType == 'user')
                    next(`/clientlogin?redirect=${to.fullPath}`)
                return;
            }
        } else {
            //不需要登录，则进行跳转
            console.info('不需要登录')
            next()
            return;
        }
    } else {
        //访问URL能路由匹配不上
        console.info('未匹配回网站首页', to.fullPath);
        next('/client/index')
        return;
    }
})
//导出路由
export default router;

